#include "lxz_config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#ifdef LXZAT_WIN32
#include <io.h>
#include <direct.h>
#include <windows.h>
#endif /* LXZAT_WIN32 */

#ifdef LXZAT_LINUX
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/stat.h>
#include <errno.h>
#endif /* LXZAT_LINUX */

#include "lxz_types.h"
#include "os_port.h"
#include "lxz_runlog.h"
#include "lxz_debug.h"
#include "lxz_ring.h"
#include "lxz_string.h"
#include "lxz_dstring.h"
#include "lxz_atc_req.h"
#include "lxz_urc_table.h"
#include "lxz_file_cmd.h"

#include "lxz_dbg_vmem.h"


typedef struct _lxzat_file_t
{
    sint32 it_id_vsp;     /* Virtual-SerialPort ID */
    sint32 it_fd_file;
    sint32 it_rw_status;
    sint32 it_nb_rxdata; /* the length of data that stored in rxbuf */
    sint32 it_nb_txdata; /* the length of data that stored in txbuf */
    sint32 it_rxbuf_size; /* The size of rxbuf */
    sint32 it_txbuf_size; /* The size of txbuf */
    uint08 * pt_rxbuf;    /* pointer of rxbuf */
    uint08 * pt_txbuf;    /* pointer of rxbuf */
    uint08 pt_open_mode[LXZAT_FILE_MODE_MAX];
    uint08 pt_file_name[LXZAT_FILE_PATH_MAX];
}lxzat_file_t;

/*
 *  Description:
 *    A structure for saving parameters and result.
 *  Note: NONE
 */
typedef struct _lxzat_resp_myfilewget_t
{
    sint32 it_fd_file; /* the 1st parameter */
    uint08 pt_fn_file[LXZAT_FILE_PATH_MAX]; /* the 2nd parameter */
} lxzat_resp_myfilewget_t;

static lxzat_file_t gs_file_pool[LXZAT_FILE_NUM_MAX+1] = {0};

static sint32 lxzat_dollar_myfileopen_init(void * pctxt);
static sint32 lxzat_dollar_myfileopen_req(void * pctxt);
static sint32 lxzat_dollar_myfileopen_resp(void * pctxt);
static sint32 lxzat_dollar_myfileopen_reset(void * pctxt);

static sint32 lxzat_dollar_myfilewrite_init(void * pctxt);
static sint32 lxzat_dollar_myfilewrite_req(void * pctxt);
static sint32 lxzat_dollar_myfilewrite_resp(void * pctxt);
static sint32 lxzat_dollar_myfilewrite_reset(void * pctxt);

static sint32 lxzat_dollar_myfileread_init(void * pctxt);
static sint32 lxzat_dollar_myfileread_req(void * pctxt);
static sint32 lxzat_dollar_myfileread_resp(void * pctxt);
static sint32 lxzat_dollar_myfileread_reset(void * pctxt);

static sint32 lxzat_dollar_myfileclose_init(void * pctxt);
static sint32 lxzat_dollar_myfileclose_req(void * pctxt);
static sint32 lxzat_dollar_myfileclose_resp(void * pctxt);
static sint32 lxzat_dollar_myfileclose_reset(void * pctxt);

static sint32 lxzat_dollar_myfilesize_init(void * pctxt);
static sint32 lxzat_dollar_myfilesize_req(void * pctxt);
static sint32 lxzat_dollar_myfilesize_resp(void * pctxt);
static sint32 lxzat_dollar_myfilesize_reset(void * pctxt);

static sint32 lxzat_dollar_myfiledelete_init(void * pctxt);
static sint32 lxzat_dollar_myfiledelete_req(void * pctxt);
static sint32 lxzat_dollar_myfiledelete_resp(void * pctxt);
static sint32 lxzat_dollar_myfiledelete_reset(void * pctxt);

static sint32 lxzat_dollar_myfilequery_init(void * pctxt);
static sint32 lxzat_dollar_myfilequery_req(void * pctxt);
static sint32 lxzat_dollar_myfilequery_resp(void * pctxt);
static sint32 lxzat_dollar_myfilequery_reset(void * pctxt);

static sint32 lxzat_dollar_myfileput_init(void * pctxt);
static sint32 lxzat_dollar_myfileput_req(void * pctxt);
static sint32 lxzat_dollar_myfileput_resp(void * pctxt);
static sint32 lxzat_dollar_myfileput_reset(void * pctxt);

static sint32 lxzat_dollar_myfileget_init(void * pctxt);
static sint32 lxzat_dollar_myfileget_req(void * pctxt);
static sint32 lxzat_dollar_myfileget_resp(void * pctxt);
static sint32 lxzat_dollar_myfileget_reset(void * pctxt);

static sint32 lxzat_dollar_myfilewget_init(void * pctxt);
static sint32 lxzat_dollar_myfilewget_req(void * pctxt);
static sint32 lxzat_dollar_myfilewget_resp(void * pctxt);
static sint32 lxzat_dollar_myfilewget_reset(void * pctxt);

static sint32 lxzat_myfilestatus_f_get(const char * p_a_filename);


/*
*  Description:
*    A caplets for hiding concrete implementation.
*  Note: 
*    Please do not change any part of current piece when you unsure.
*/
const lxzat_info_t lai_dollar_myfileopen =
{
    LXZAT_TYPE_RANGE_LOCAL|LXZAT_TYPE_STRING_EXTEND,
	"$MYFILEOPEN", 
	lxzat_dollar_myfileopen_init,
	lxzat_dollar_myfileopen_req,
	lxzat_dollar_myfileopen_resp,
	lxzat_dollar_myfileopen_reset
};

/*
*  Description:
*    A caplets for hiding concrete implementation.
*  Note: 
*    Please do not change any part of current piece when you unsure.
*/
const lxzat_info_t lai_dollar_myfilewrite =
{
    LXZAT_TYPE_RANGE_LOCAL|LXZAT_TYPE_STRING_EXTEND,
	"$MYFILEWRITE", 
	lxzat_dollar_myfilewrite_init,
	lxzat_dollar_myfilewrite_req,
	lxzat_dollar_myfilewrite_resp,
	lxzat_dollar_myfilewrite_reset
};

/*
*  Description:
*    A caplets for hiding concrete implementation.
*  Note: 
*    Please do not change any part of current piece when you unsure.
*/
const lxzat_info_t lai_dollar_myfileread =
{
    LXZAT_TYPE_RANGE_LOCAL|LXZAT_TYPE_STRING_EXTEND,
	"$MYFILEREAD", 
	lxzat_dollar_myfileread_init,
	lxzat_dollar_myfileread_req,
	lxzat_dollar_myfileread_resp,
	lxzat_dollar_myfileread_reset
};

/*
*  Description:
*    A caplets for hiding concrete implementation.
*  Note: 
*    Please do not change any part of current piece when you unsure.
*/
const lxzat_info_t lai_dollar_myfileclose =
{
    LXZAT_TYPE_RANGE_LOCAL|LXZAT_TYPE_STRING_EXTEND,
	"$MYFILECLOSE", 
	lxzat_dollar_myfileclose_init,
	lxzat_dollar_myfileclose_req,
	lxzat_dollar_myfileclose_resp,
	lxzat_dollar_myfileclose_reset
};

/*
*  Description:
*    A caplets for hiding concrete implementation.
*  Note: 
*    Please do not change any part of current piece when you unsure.
*/
const lxzat_info_t lai_dollar_myfilesize =
{
    LXZAT_TYPE_RANGE_LOCAL|LXZAT_TYPE_STRING_EXTEND,
	"$MYFILESIZE", 
	lxzat_dollar_myfilesize_init,
	lxzat_dollar_myfilesize_req,
	lxzat_dollar_myfilesize_resp,
	lxzat_dollar_myfilesize_reset
};

/*
*  Description:
*    A caplets for hiding concrete implementation.
*  Note: 
*    Please do not change any part of current piece when you unsure.
*/
const lxzat_info_t lai_dollar_myfiledelete =
{
    LXZAT_TYPE_RANGE_LOCAL|LXZAT_TYPE_STRING_EXTEND,
	"$MYFILEDELETE", 
	lxzat_dollar_myfiledelete_init,
	lxzat_dollar_myfiledelete_req,
	lxzat_dollar_myfiledelete_resp,
	lxzat_dollar_myfiledelete_reset
};

/*
*  Description:
*    A caplets for hiding concrete implementation.
*  Note: 
*    Please do not change any part of current piece when you unsure.
*/
const lxzat_info_t lai_dollar_myfilequery =
{
    LXZAT_TYPE_RANGE_LOCAL|LXZAT_TYPE_STRING_EXTEND,
	"$MYFILEQUERY", 
	lxzat_dollar_myfilequery_init,
	lxzat_dollar_myfilequery_req,
	lxzat_dollar_myfilequery_resp,
	lxzat_dollar_myfilequery_reset
};

/*
*  Description:
*    A caplets for hiding concrete implementation.
*  Note: 
*    Please do not change any part of current piece when you unsure.
*/
const lxzat_info_t lai_dollar_myfileput =
{
    LXZAT_TYPE_RANGE_LOCAL|LXZAT_TYPE_STRING_EXTEND,
	"$MYFILEPUT", 
	lxzat_dollar_myfileput_init,
	lxzat_dollar_myfileput_req,
	lxzat_dollar_myfileput_resp,
	lxzat_dollar_myfileput_reset
};

/*
*  Description:
*    A caplets for hiding concrete implementation.
*  Note: 
*    Please do not change any part of current piece when you unsure.
*/
const lxzat_info_t lai_dollar_myfileget =
{
    LXZAT_TYPE_RANGE_LOCAL|LXZAT_TYPE_STRING_EXTEND,
	"$MYFILEGET", 
	lxzat_dollar_myfileget_init,
	lxzat_dollar_myfileget_req,
	lxzat_dollar_myfileget_resp,
	lxzat_dollar_myfileget_reset
};

/*
*  Description:
*    A caplets for hiding concrete implementation.
*  Note: 
*    Please do not change any part of current piece when you unsure.
*/
const lxzat_info_t lai_dollar_myfilewget =
{
    LXZAT_TYPE_RANGE_LOCAL|LXZAT_TYPE_STRING_EXTEND,
	"$MYFILEWGET", 
	lxzat_dollar_myfilewget_init,
	lxzat_dollar_myfilewget_req,
	lxzat_dollar_myfilewget_resp,
	lxzat_dollar_myfilewget_reset
};

/*
*  Description:
*    The 2nd step of "AT$MYFILEWRITE".
*  Param: p_data_buf, the pointer of data buffer;
*  Param: i_data_len, the length of data in buffer;
*  Param: i_end_flag, the end-flag recved or not;
*  Param: pctxt, the descriptor of serial-port;
*  Return: the number of bytes have been sent;
*  Note: NONE
*/
static sint32 lxzdata_dollar_myfilewrite_handler(sint32 i_end_flag, void * pctxt)
{
    sint32 i_nb_write = 0;
	
    sint32 i_file_id = 0;
    sint32 i_nb_read = 0;
    sint32 i_nb_tryread = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
	
    p_cur_spcfg = (lxzat_spcfg_t *)pctxt;
    if (p_cur_spcfg == NULL)
    {
        OS_DBG_LOG(("lxzdata_dollar_myfilewrite_handler,p_cur_spcfg is NULL!\r\n"));
        return i_nb_write;
    }
	
    i_nb_read = lxz_ring_f_getoption(p_cur_spcfg->pt_rx_ring, E_LRO_DATA_SIZE);
    if (i_nb_read < p_cur_spcfg->i_jump_length)
    {
        OS_DBG_LOG(("lxzdata_dollar_myfilewrite_handler,i_cache_len=%d,i_expect_len=%d\r\n",i_nb_read, 
            p_cur_spcfg->i_jump_length));
        return i_nb_write;
    }
	
    p_cur_datacfg = p_cur_spcfg->pt_di_cfg;
    if (p_cur_datacfg == NULL)
    {
        OS_DBG_LOG(("lxzdata_dollar_myfilewrite_handler,p_cur_datacfg is NULL!\r\n"));
        return i_nb_write;
    }
	
    i_file_id = ((sint32)(p_cur_datacfg->p_param_ctxt));
    p_cur_filectxt = &(gs_file_pool[i_file_id]);
    if (p_cur_filectxt->it_rw_status != E_LFRWS_WRITING)
    {
        OS_DBG_LOG(("lxzat_dollar_myfilewrite_req,it_rw_status=%d\r\n", p_cur_filectxt->it_rw_status));
        return E_LEC_ERR_NOT_ALLOWED;
    }
	
    i_nb_tryread = p_cur_spcfg->i_jump_length;
    i_nb_read = lxz_ring_f_read(p_cur_spcfg->pt_rx_ring, p_cur_filectxt->pt_txbuf, i_nb_tryread);
    if (i_nb_read != i_nb_tryread)
    {
        OS_DBG_LOG(("lxzdata_dollar_myfilewrite_handler,i_read_len=%d,i_nb_tryread=%d\r\n",i_nb_read,
            i_nb_tryread));
        return i_nb_write;
    }
	
    fwrite(p_cur_filectxt->pt_txbuf, 1, i_nb_tryread, (FILE *)p_cur_filectxt->it_fd_file);
    p_cur_filectxt->it_rw_status = E_LFRWS_OPEN;
    i_nb_write = i_nb_tryread;
	
    free(p_cur_datacfg);
    p_cur_spcfg->pt_di_cfg = NULL;
	
    lxzat_rsp_f_printf(p_cur_spcfg->i_vsp_id, LXZAT_RSP_MODE_P1N1, "OK");
	
    lxzat_spcomm_f_clear(p_cur_spcfg->i_vsp_id);
    lxzat_spcomm_f_ChangeWorkMode(p_cur_spcfg->i_vsp_id,
        E_LSMX_ATC, LXZAT_END_CHAR_CR, LXZAT_END_CHAR_LF);
    return i_nb_write;
}

/*
*  Description:
*    The initialize handler of "AT$MYFILEOPEN".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileopen_init(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The request handler of "AT$MYFILEOPEN".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileopen_req(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    sint32 i_view_mode = 0;
    sint32 i_op_status = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfileopen_req,p_lxzat_ctxt is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfileopen_req,p_cur_req or p_cur_spcfg is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            char ll_ffn_buf[LXZAT_FILE_PATH_MAX + 20];
            if (p_cur_req->it_nb_param != 3)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileopen_req,i_nb_param:%d\r\n",p_cur_req->it_nb_param));
                return E_LEC_ERR_P_NB_PARAM;
            }
			
            if (PARAM1_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileopen_req,PARAM1_TYPE error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            if (PARAM2_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_STRING)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileopen_req,PARAM2_TYPE error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            if (PARAM3_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_STRING)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileopen_req,PARAM3_TYPE error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            i_file_id = PARAM1(p_cur_req).it_number;
            if(i_file_id < 0 || i_file_id > LXZAT_FILE_NUM_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileopen_req,i_file_id=%d\r\n",i_file_id));
                return E_LEC_ERR_P_RANGE;
            }
			
            p_cur_filectxt = &(gs_file_pool[i_file_id]);
            if (p_cur_filectxt->it_rw_status != E_LFRWS_IDLE)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileopen_req,it_rw_status=%d\r\n",p_cur_filectxt->it_rw_status));
                return E_LEC_ERR_NOT_ALLOWED;
            }
			
            memset(p_cur_filectxt, 0, sizeof(lxzat_file_t));
            p_cur_filectxt->pt_rxbuf = (uint08 *)malloc(LXZAT_FILE_TRYREAD_MAX);
            if(p_cur_filectxt->pt_rxbuf == NULL)
            {
                p_cur_filectxt->it_rw_status = E_LFRWS_IDLE;
				
                OS_DBG_LOG(("lxzat_dollar_myfileopen_req,pt_rxbuf malloc error\r\n"));
                return E_LEC_ERR_MEM_MALLOC;
            }
			
            p_cur_filectxt->pt_txbuf = (uint08 *)malloc(LXZAT_FILE_TRYWRITE_MAX);
            if(p_cur_filectxt->pt_txbuf == NULL)
            {
                free(p_cur_filectxt->pt_rxbuf);
                p_cur_filectxt->pt_rxbuf = NULL;
				
                OS_DBG_LOG(("lxzat_dollar_myfileopen_req,pt_txbuf malloc error\r\n"));
                return E_LEC_ERR_MEM_MALLOC;
            }
			
            p_cur_filectxt->it_rxbuf_size = LXZAT_FILE_TRYREAD_MAX;
            p_cur_filectxt->it_txbuf_size = LXZAT_FILE_TRYWRITE_MAX;
            memset(p_cur_filectxt->pt_rxbuf, 0, LXZAT_FILE_TRYREAD_MAX);
            memset(p_cur_filectxt->pt_txbuf, 0, LXZAT_FILE_TRYWRITE_MAX);
            memset(p_cur_filectxt->pt_file_name, 0, LXZAT_FILE_PATH_MAX);
            lxz_dstring_get(PARAM2(p_cur_req).pt_str, p_cur_filectxt->pt_file_name, LXZAT_FILE_PATH_MAX);
            lxz_dstring_get(PARAM3(p_cur_req).pt_str, p_cur_filectxt->pt_open_mode, LXZAT_FILE_MODE_MAX);
            if (E_LFRWS_IDLE != lxzat_myfilestatus_f_get(p_cur_filectxt->pt_file_name))
            {
                free(p_cur_filectxt->pt_rxbuf);
                p_cur_filectxt->pt_rxbuf = NULL;
				
                free(p_cur_filectxt->pt_txbuf);
                p_cur_filectxt->pt_txbuf = NULL;
				
                OS_DBG_LOG(("lxzat_dollar_myfileopen_req,lxzat_myfilestatus_f_get,error\r\n"));
                return E_LEC_ERR_NOT_ALLOWED;
            }
			
            if (access(LXZAT_FILE_ROOT_DIRECTORY, F_OK) < 0)
            {
                sprintf(ll_ffn_buf, "%s", LXZAT_FILE_ROOT_DIRECTORY);
#ifdef LXZAT_WIN32
                _mkdir(ll_ffn_buf);
#else
                umask(0);
                mkdir(ll_ffn_buf, 0775);
#endif
                
            }
			
            sprintf(ll_ffn_buf, "%s/%s", LXZAT_FILE_ROOT_DIRECTORY, p_cur_filectxt->pt_file_name);
            lxz_string_f_strlwr(p_cur_filectxt->pt_open_mode, strlen(p_cur_filectxt->pt_open_mode));
            p_cur_filectxt->it_fd_file = (sint32)fopen(ll_ffn_buf, p_cur_filectxt->pt_open_mode);
            if (((FILE *)p_cur_filectxt->it_fd_file) == NULL)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileopen_req,fopen,fn=%s,mode=%s\r\n",p_cur_filectxt->pt_file_name, 
                    p_cur_filectxt->pt_open_mode));
				
                free(p_cur_filectxt->pt_rxbuf);
                p_cur_filectxt->pt_rxbuf = NULL;
				
                free(p_cur_filectxt->pt_txbuf);
                p_cur_filectxt->pt_txbuf = NULL;
				
                return E_LEC_ERR_NOK;
            }
			
            p_cur_filectxt->it_rw_status = E_LFRWS_OPEN;
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            break;
        }
    default:
        {
            break;
        }
    }
	
    return i_err_code;
}


/*
*  Description:
*    The response handler of "AT$MYFILEOPEN".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileopen_resp(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
	
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    sint32 i_op_status = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfileopen_resp,p_lxzat_ctxt is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfileopen_resp,p_cur_req or p_cur_spcfg is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            i_file_id = PARAM1(p_cur_req).it_number;
            if(i_file_id < 0 || i_file_id > LXZAT_FILE_NUM_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileopen_resp,i_file_id=%d\r\n",i_file_id));
                return E_LEC_ERR_UNSUPPORTED;
            }
			
            p_cur_filectxt = &(gs_file_pool[i_file_id]);
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: %d,%d",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                i_file_id,
                p_cur_filectxt->it_rw_status);
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: <FileID>,<\"FileName\">,<\"OpenMode\">",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword);
            break;
        }
		
    default:
        {
            break;
        }
    }
	
    return i_err_code;
}


/*
*  Description:
*    The reset handler of "AT$MYFILEOPEN".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileopen_reset(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The initialize handler of "AT$MYFILEWRITE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilewrite_init(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The request handler of "AT$MYFILEWRITE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilewrite_req(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
	
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    sint32 i_nb_trywrite = 0;
    sint32 i_op_status = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfilewrite_req,p_lxzat_ctxt is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfilewrite_req,p_cur_req or p_cur_spcfg is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            if (p_cur_req->it_nb_param != 2)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewrite_req,i_nb_param=%d\r\n",p_cur_req->it_nb_param));
                return E_LEC_ERR_P_NB_PARAM;
            }
			
            if (PARAM1_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewrite_req,param1 type error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            if (PARAM2_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewrite_req,param2 type error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            i_file_id= PARAM1(p_cur_req).it_number;
            if ((i_file_id < 0) || (i_file_id > LXZAT_FILE_NUM_MAX))
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewrite_req,i_file_id=%d\r\n",i_file_id));
                return E_LEC_ERR_P_RANGE;
            }
			
            p_cur_filectxt = &(gs_file_pool[i_file_id]);
            if (p_cur_filectxt->it_rw_status != E_LFRWS_OPEN)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewrite_req,it_rw_status=%d\r\n", p_cur_filectxt->it_rw_status));
                return E_LEC_ERR_NOT_ALLOWED;
            }
            
            i_nb_trywrite= PARAM2(p_cur_req).it_number;
            if(i_nb_trywrite < LXZAT_FILE_TRYWRITE_MIN || i_nb_trywrite > LXZAT_FILE_TRYWRITE_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewrite_req,i_nb_trywrite=%d\r\n",i_nb_trywrite));
                return E_LEC_ERR_P_RANGE;
            }
			
            p_cur_datacfg = (lxzdata_info_t *)malloc(sizeof(lxzdata_info_t));
            if (p_cur_datacfg == NULL)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewrite_req,p_cur_datacfg malloc error!\r\n"));
                return E_LEC_ERR_MEM_MALLOC;
            }
			
            memset(p_cur_datacfg, 0, sizeof(lxzdata_info_t));
            p_cur_datacfg->fp_data_handler = lxzdata_dollar_myfilewrite_handler;
            p_cur_datacfg->p_param_ctxt = (uint32 *)i_file_id;
			
            p_cur_spcfg->i_jump_length = i_nb_trywrite;
            p_cur_spcfg->pt_di_cfg = p_cur_datacfg;
            p_cur_spcfg->i_echo_auxiliary = LXZAT_FALSE;
            lxzat_spcomm_f_clear(p_cur_spcfg->i_vsp_id);
            lxzat_spcomm_f_ChangeWorkMode(p_cur_spcfg->i_vsp_id,
				E_LSMA_FIXED_LEN, 0, 0);
			
            p_cur_filectxt->it_rw_status = E_LFRWS_WRITING;
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
				LXZAT_RSP_MODE_P1N1,
				"%s","> ");
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            break;
        }
    default:
        {
            i_err_code = E_LEC_ERR_UNSUPPORTED;
            break;
        }
    }
	
    return i_err_code;
}


/*
*  Description:
*    The response handler of "AT$MYFILEWRITE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilewrite_resp(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfilewrite_resp,p_lxzat_ctxt is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfilewrite_resp,p_cur_req or p_cur_spcfg is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            i_err_code = E_LEC_OK_NOTHING;
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: <FileID>,<TryWriteLength>",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword);
			
            break;
        }
    default:
        {
            i_err_code = E_LEC_ERR_UNKNOWN;
            break;
        }
    }
	
    return i_err_code;
}


/*
*  Description:
*    The reset handler of "AT$MYFILEWRITE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilewrite_reset(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The initialize handler of "AT$MYFILEREAD".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileread_init(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The request handler of "AT$MYFILEREAD".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileread_req(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    sint32 i_read_len = 0;
    sint32 i_nb_tryread = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfileread_req,p_lxzat_ctxt is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfileread_req,p_cur_req or p_cur_spcfg is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            if (p_cur_req->it_nb_param != 2)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileread_req,i_nb_param=%d\r\n",p_cur_req->it_nb_param));
                return E_LEC_ERR_P_NB_PARAM;
            }
			
            if (PARAM1_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileread_req,param1 type is error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            if (PARAM2_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileread_req,param2 type is error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            i_file_id = PARAM1(p_cur_req).it_number;
            if(i_file_id < 0 || i_file_id > LXZAT_FILE_NUM_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileread_req,i_file_id=%d\r\n",i_file_id));
                return E_LEC_ERR_P_RANGE;
            }
			
            i_nb_tryread= PARAM2(p_cur_req).it_number;
            if(i_nb_tryread < LXZAT_FILE_TRYREAD_MIN || i_nb_tryread > LXZAT_FILE_TRYREAD_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileread_req,i_nb_tryread=%d\r\n",i_nb_tryread));
                return E_LEC_ERR_P_RANGE;
            }
			
            p_cur_filectxt = &(gs_file_pool[i_file_id]);
            if (p_cur_filectxt->it_rw_status < E_LFRWS_OPEN)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileread_req,file is not open\r\n"));
                return E_LEC_ERR_NOT_ALLOWED;
            }
			
            i_read_len = fread(p_cur_filectxt->pt_rxbuf, 1, i_nb_tryread, (FILE *)p_cur_filectxt->it_fd_file);
            p_cur_filectxt->it_nb_rxdata = i_read_len;
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            break;
        }
    default:
        {
            i_err_code = E_LEC_ERR_UNSUPPORTED;
            break;
        }
    }
	
    return i_err_code;
}


/*
*  Description:
*    The response handler of "AT$MYFILEREAD".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileread_resp(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    sint32 i_read_len = 0;
    sint32 i_nb_tryread = 0;
	
    uint08 * p_put_buf = NULL;
    sint32 i_put_len = 0;
	
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfileread_resp,p_lxzat_ctxt is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfileread_resp,p_cur_req or p_cur_spcfg is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            i_file_id = PARAM1(p_cur_req).it_number;
            if(i_file_id < 0 || i_file_id > LXZAT_FILE_NUM_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileread_resp,i_file_id=%d\r\n",i_file_id));
                return E_LEC_ERR_UNSUPPORTED;
            }
			
            i_nb_tryread= PARAM2(p_cur_req).it_number;
            if(i_nb_tryread < LXZAT_FILE_TRYREAD_MIN || i_nb_tryread > LXZAT_FILE_TRYREAD_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileread_resp, i_nb_tryread=%d\r\n",i_nb_tryread));
                return E_LEC_ERR_UNSUPPORTED;
            }
			
            p_cur_filectxt = &(gs_file_pool[i_file_id]);
            i_read_len = p_cur_filectxt->it_nb_rxdata;
            if (i_read_len > 0)
            {
                lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                    LXZAT_RSP_MODE_P1N1,
                    "%s: %d,%d",
                    p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                    i_file_id,
                    i_read_len);
                lxzat_rsp_f_printfbin(p_lxzat_ctxt->it_vsp_id,
                    LXZAT_RSP_MODE_P0N0,
                    p_cur_filectxt->pt_rxbuf,
                    i_read_len);
            }
            else
            {
                lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                    LXZAT_RSP_MODE_P1N0,
                    "%s: %d,%d",
                    p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                    i_file_id,
                    0);
            }
			
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: <FileID>,<TryReadLength>",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword);
			
            break;
        }
		
    default:
        {
            break;
        }
    }
	
    return i_err_code;
}


/*
*  Description:
*    The reset handler of "AT$MYFILEREAD".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileread_reset(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The initialize handler of "AT$MYFILECLOSE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileclose_init(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The request handler of "AT$MYFILECLOSE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileclose_req(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    sint32 i_view_mode = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfileclose_req,p_lxzat_ctxt is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfileclose_req,p_cur_req or p_cur_spcfg is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            if (p_cur_req->it_nb_param != 1)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileclose_req,i_nb_param=%d\r\n",p_cur_req->it_nb_param));
                return E_LEC_ERR_P_NB_PARAM;
            }
			
            if (PARAM1_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileclose_req,param1 type error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            i_file_id = PARAM1(p_cur_req).it_number;
            if(i_file_id < 0 || i_file_id > LXZAT_FILE_NUM_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileclose_req,i_file_id=%d\r\n",i_file_id));
                return E_LEC_ERR_P_RANGE;
            }
			
            p_cur_filectxt = &(gs_file_pool[i_file_id]);
            if(p_cur_filectxt->pt_rxbuf != NULL)
            {
                free(p_cur_filectxt->pt_rxbuf);
                p_cur_filectxt->pt_rxbuf = NULL;
            }
			
            if(p_cur_filectxt->pt_txbuf != NULL)
            {
                free(p_cur_filectxt->pt_txbuf);
                p_cur_filectxt->pt_txbuf = NULL;
            }
			
            if(((FILE *)p_cur_filectxt->it_fd_file) != NULL)
            {
                fclose((FILE *)p_cur_filectxt->it_fd_file);
                p_cur_filectxt->it_fd_file = 0;
            }
			
            memset(p_cur_filectxt, 0, sizeof(lxzat_file_t));
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            break;
        }
    default:
        {
            i_err_code = E_LEC_ERR_UNSUPPORTED;
            break;
        }
    }
	
    return i_err_code;
}

/*
*  Description:
*    The response handler of "AT$MYFILECLOSE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileclose_resp(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfileclose_resp,p_lxzat_ctxt is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfileclose_resp,p_cur_req or p_cur_spcfg is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: <FileID>",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword);
			
            break;
        }
    default:
        {
            i_err_code = E_LEC_ERR_UNKNOWN;
            break;
        }
    }
	
    return i_err_code;
}

/*
*  Description:
*    The reset handler of "AT$MYFILECLOSE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileclose_reset(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The initialize handler of "AT$MYFILESIZE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilesize_init(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The request handler of "AT$MYFILESIZE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilesize_req(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The response handler of "AT$MYFILESIZE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilesize_resp(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
	
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    sint32 i_nb_trywrite = 0;
    sint32 i_op_status = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfilesize_resp,p_lxzat_ctxt is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfilesize_resp,p_cur_req or p_cur_spcfg is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            sint32 i_file_size = 0;
            sint32 i_cur_posi = 0;
			
            if (p_cur_req->it_nb_param != 1)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilesize_resp,i_nb_param=%d\r\n",p_cur_req->it_nb_param));
                return E_LEC_ERR_P_NB_PARAM;
            }
			
            if (PARAM1_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilesize_resp,param1 type error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            i_file_id = PARAM1(p_cur_req).it_number;
            if(i_file_id < 0 || i_file_id > LXZAT_FILE_NUM_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilesize_resp,i_file_id=%d\r\n",i_file_id));
                return E_LEC_ERR_P_RANGE;
            }
			
            p_cur_filectxt = &(gs_file_pool[i_file_id]);
            if (p_cur_filectxt->it_rw_status < E_LFRWS_OPEN)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewrite_req,it_rw_status=%d\r\n", p_cur_filectxt->it_rw_status));
                return E_LEC_ERR_NOT_ALLOWED;
            }
			
            i_cur_posi = ftell((FILE *)p_cur_filectxt->it_fd_file);
            fseek((FILE *)p_cur_filectxt->it_fd_file, 0, SEEK_END);
            i_file_size = ftell((FILE *)p_cur_filectxt->it_fd_file);
            fseek((FILE *)p_cur_filectxt->it_fd_file, i_cur_posi, SEEK_SET);
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: %d,%d",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                i_file_id,
                i_file_size);
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: <FileID>",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword);
			
            break;
        }
    default:
        {
            i_err_code = E_LEC_ERR_UNKNOWN;
            break;
        }
    }
	
    return i_err_code;
}

/*
*  Description:
*    The reset handler of "AT$MYFILESIZE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilesize_reset(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The initialize handler of "AT$MYFILEDELETE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfiledelete_init(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The request handler of "AT$MYFILEDELETE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfiledelete_req(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfiledelete_req,p_lxzat_ctxt is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfiledelete_req,p_cur_req or p_cur_spcfg is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            char ll_fn_buf[LXZAT_FILE_PATH_MAX];
            char ll_sh_buf[LXZAT_FILE_PATH_MAX + 64];
            if (p_cur_req->it_nb_param != 1)
            {
                OS_DBG_LOG(("lxzat_dollar_myfiledelete_req,i_nb_param=%d\r\n",p_cur_req->it_nb_param));
                return E_LEC_ERR_P_NB_PARAM;
            }
			
            if (PARAM1_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_STRING)
            {
                OS_DBG_LOG(("lxzat_dollar_myfiledelete_req,PARAM1_TYPE error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            memset(ll_fn_buf, 0, sizeof(ll_fn_buf));
            lxz_dstring_get(PARAM1(p_cur_req).pt_str, ll_fn_buf, LXZAT_FILE_PATH_MAX);
            if (E_LFRWS_IDLE != lxzat_myfilestatus_f_get(ll_fn_buf))
            {
                OS_DBG_LOG(("lxzat_dollar_myfiledelete_req,lxzat_myfilestatus_f_get,error\r\n"));
                return E_LEC_ERR_NOT_ALLOWED;
            }
            
            if (strlen(ll_fn_buf) > 0)
            {
                sprintf(ll_sh_buf, "%s/%s", LXZAT_FILE_ROOT_DIRECTORY, ll_fn_buf);
				remove(ll_sh_buf);
            }
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            break;
        }
    default:
        {
            break;
        }
    }
	
    return i_err_code;
}


/*
*  Description:
*    The response handler of "AT$MYFILEDELETE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfiledelete_resp(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
	
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfiledelete_resp,p_lxzat_ctxt is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfiledelete_resp,p_cur_req or p_cur_spcfg is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: <\"FileName\">",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword);
			
            break;
        }
    default:
        {
            i_err_code = E_LEC_ERR_UNKNOWN;
            break;
        }
    }
	
    return i_err_code;
}


/*
*  Description:
*    The reset handler of "AT$MYFILEDELETE".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfiledelete_reset(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The initialize handler of "AT$MYFILEQUERY".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilequery_init(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The request handler of "AT$MYFILEQUERY".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilequery_req(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The response handler of "AT$MYFILEQUERY".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilequery_resp(void * pctxt)
{
	sint32 i_err_code = E_LEC_OK;
	sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
	
	lxzat_req_t * p_cur_req = NULL;
	lxzat_spcfg_t * p_cur_spcfg = NULL;
	lxzdata_info_t * p_cur_datacfg = NULL;
	lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
	sint32 i_file_id = 0;
	sint32 i_nb_trywrite = 0;
	sint32 i_op_status = 0;
	lxzat_file_t * p_cur_filectxt = NULL;
	
	if (p_lxzat_ctxt == NULL)
	{
		OS_DBG_LOG(("lxzat_dollar_myfilequery_resp,p_lxzat_ctxt is NULL!\r\n"));
		return E_LEC_ERR_UNKNOWN;
	}
	
	p_cur_req = p_lxzat_ctxt->pt_req_data;
	p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
	if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
	{
		OS_DBG_LOG(("lxzat_dollar_myfilequery_resp,p_cur_req or p_cur_spcfg is NULL!\r\n"));
		return E_LEC_ERR_UNKNOWN;
	}
	
	i_req_type = p_cur_req->it_req_type;
	switch(i_req_type)
	{
	case LXZAT_REQ_TYPE_SET:
		{
			sint32 i_query_type = 0;
			
			
			if (p_cur_req->it_nb_param != 2)
			{
				OS_DBG_LOG(("lxzat_dollar_myfilequery_resp,i_nb_param=%d\r\n",p_cur_req->it_nb_param));
				return E_LEC_ERR_P_NB_PARAM;
			}
			
			if (PARAM1_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
			{
				OS_DBG_LOG(("lxzat_dollar_myfilequery_resp,param1 type error!\r\n"));
				return E_LEC_ERR_P_TYPE;
			}
			
			if (PARAM2_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
			{
				OS_DBG_LOG(("lxzat_dollar_myfilequery_resp,param2 type error!\r\n"));
				return E_LEC_ERR_P_TYPE;
			}
			
			i_file_id = PARAM1(p_cur_req).it_number;
			if(i_file_id < 0 || i_file_id > LXZAT_FILE_NUM_MAX)
			{
				OS_DBG_LOG(("lxzat_dollar_myfilequery_resp,i_file_id=%d\r\n",i_file_id));
				return E_LEC_ERR_P_RANGE;
			}
			
			p_cur_filectxt = &(gs_file_pool[i_file_id]);
			if (p_cur_filectxt->it_rw_status < E_LFRWS_OPEN)
			{
				OS_DBG_LOG(("lxzat_dollar_myfilequery_resp,it_rw_status=%d\r\n", p_cur_filectxt->it_rw_status));
				return E_LEC_ERR_NOT_ALLOWED;
			}

            i_query_type = PARAM2(p_cur_req).it_number;
            switch(i_query_type)
            {
                case E_LFQT_FILESIZE:
                {
                    sint32 i_cur_posi = 0;
                    sint32 i_file_size = 0;
                    
                    i_cur_posi = ftell((FILE *)p_cur_filectxt->it_fd_file);
                    fseek((FILE *)p_cur_filectxt->it_fd_file, 0, SEEK_END);
                    i_file_size = ftell((FILE *)p_cur_filectxt->it_fd_file);
                    fseek((FILE *)p_cur_filectxt->it_fd_file, i_cur_posi, SEEK_SET);
                    lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                        LXZAT_RSP_MODE_P1N0,
                        "%s: %d,%d,%d",
                        p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                        i_file_id,
                        i_query_type,
                        i_file_size);
                    break;
                }
                
                case E_LFQT_FILENAME:
                {
                    lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                        LXZAT_RSP_MODE_P1N0,
                        "%s: %d,%d,\"%s\"",
                        p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                        i_file_id,
                        i_query_type,
                        p_cur_filectxt->pt_file_name);

                    break;
                }

                default:
                {
                    break;
                }
            }

			break;
		}
	case LXZAT_REQ_TYPE_READ:
		{
			break;
		}
	case LXZAT_REQ_TYPE_TEST:
		{
			lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
				LXZAT_RSP_MODE_P1N0,
				"%s: <FileID>,<QueryType>",
				p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword);
			
			break;
		}
	default:
		{
			i_err_code = E_LEC_ERR_UNKNOWN;
			break;
		}
	}
	
	return i_err_code;
}


/*
*  Description:
*    The reset handler of "AT$MYFILEQUERY".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilequery_reset(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The initialize handler of "AT$MYFILEPUT".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileput_init(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The request handler of "AT$MYFILEPUT".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileput_req(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    sint32 i_string_type = 0;
    sint32 i_string_len = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfileput_req,p_lxzat_ctxt is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfileput_req,p_cur_req or p_cur_spcfg is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            if (p_cur_req->it_nb_param != 3)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileput_req,i_nb_param=%d\r\n",p_cur_req->it_nb_param));
                return E_LEC_ERR_P_NB_PARAM;
            }
			
            if (PARAM1_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileput_req,param1 type error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            if (PARAM2_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileput_req,param2 type error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            if (PARAM3_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_STRING)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileput_req,param3 type error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            i_file_id = PARAM1(p_cur_req).it_number;
            if(i_file_id < 0 || i_file_id > LXZAT_FILE_NUM_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileput_req,i_file_id=%d\r\n",i_file_id));
                return E_LEC_ERR_P_RANGE;
            }
			
            i_string_type= PARAM2(p_cur_req).it_number;
            if(i_string_type < 0 || i_string_type > 1)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileput_req,i_string_type=%d\r\n",i_string_type));
                return E_LEC_SGCC_PARAM_NOT_CORRECT;
            }
			
            p_cur_filectxt = &(gs_file_pool[i_file_id]);
            if (p_cur_filectxt->it_rw_status != E_LFRWS_OPEN)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileput_req,file is not open\r\n"));
                return E_LEC_ERR_P_OTHER;
            }
			
            i_string_len = lxz_dstring_getlength(PARAM3(p_cur_req).pt_str);
            if(i_string_len < LXZAT_FILE_TRYWRITE_MIN || i_string_len > LXZAT_FILE_TRYWRITE_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileput_req,i_nb_trywrite=%d\r\n",i_string_len));
                return E_LEC_ERR_P_LENGTH;
            }
			
            memset(p_cur_filectxt->pt_txbuf, 0, p_cur_filectxt->it_txbuf_size);
            lxz_dstring_get(PARAM3(p_cur_req).pt_str, p_cur_filectxt->pt_txbuf, p_cur_filectxt->it_txbuf_size);
            p_cur_filectxt->it_nb_txdata = strlen(p_cur_filectxt->pt_txbuf);
			
            if (i_string_type == 1)
            {
                sint32 i_op_status = 0;
				
                i_op_status = lxz_string_f_hex2bin(p_cur_filectxt->pt_txbuf, p_cur_filectxt->it_nb_txdata, 
					p_cur_filectxt->pt_txbuf);
                if (0 == i_op_status)
                {
                    OS_DBG_LOG(("lxzat_dollar_myfileput_req,lxz_string_f_hex2bin error!\r\n"));
                    return E_LEC_ERR_P_OTHER;
                }
				
                p_cur_filectxt->it_nb_txdata = (p_cur_filectxt->it_nb_txdata >> 1);
            }
			
            fwrite(p_cur_filectxt->pt_txbuf, 1, p_cur_filectxt->it_nb_txdata, (FILE *)p_cur_filectxt->it_fd_file);
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            break;
        }
    default:
        {
            i_err_code = E_LEC_ERR_UNSUPPORTED;
            break;
        }
    }
	
    return i_err_code;
}

/*
*  Description:
*    The response handler of "AT$MYFILEPUT".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileput_resp(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    sint32 i_read_len = 0;
    sint32 i_data_len = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfileput_resp,p_lxzat_ctxt is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfileput_resp,p_cur_req or p_cur_spcfg is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            sint32 i_cur_offset = 0;
			
            i_file_id = PARAM1(p_cur_req).it_number;
            if(i_file_id < 0 || i_file_id > LXZAT_FILE_NUM_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileread_resp,i_file_id=%d\r\n",i_file_id));
                return E_LEC_ERR_P_RANGE;
            }
			
            p_cur_filectxt = &(gs_file_pool[i_file_id]);
            i_cur_offset = ftell((FILE *)p_cur_filectxt->it_fd_file);
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: %d,%d,%d",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                i_file_id,
                p_cur_filectxt->it_nb_txdata,
                i_cur_offset);
			
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: <FileID>,<StringType>,<\"StringContent\">",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword);
			
            break;
        }
    default:
        {
            i_err_code = E_LEC_ERR_UNKNOWN;
            break;
        }
    }
	
    return i_err_code;
}

/*
*  Description:
*    The reset handler of "AT$MYFILEPUT".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileput_reset(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The initialize handler of "AT$MYFILEGET".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileget_init(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The request handler of "AT$MYFILEGET".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileget_req(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    sint32 i_read_len = 0;
    sint32 i_view_mode = 0;
    sint32 i_nb_tryread = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfileget_req,p_lxzat_ctxt is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfileget_req,p_cur_req or p_cur_spcfg is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            if (p_cur_req->it_nb_param != 3)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileget_req,i_nb_param=%d\r\n",p_cur_req->it_nb_param));
                return E_LEC_ERR_P_NB_PARAM;
            }
			
            if (PARAM1_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileget_req,param1 type is error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            if (PARAM2_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileget_req,param2 type is error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            if (PARAM3_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_NUMERIC)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileget_req,param3 type is error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }
			
            i_file_id = PARAM1(p_cur_req).it_number;
            if(i_file_id < 0 || i_file_id > LXZAT_FILE_NUM_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileget_req,i_file_id=%d\r\n",i_file_id));
                return E_LEC_ERR_P_RANGE;
            }
			
            i_view_mode = PARAM2(p_cur_req).it_number;
            if(i_view_mode < 0 || i_view_mode > 1)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileget_req,i_view_mode=%d\r\n",i_view_mode));
                return E_LEC_ERR_P_RANGE;
            }
			
            i_nb_tryread= PARAM3(p_cur_req).it_number;
            if(i_nb_tryread < LXZAT_FILE_TRYREAD_MIN || i_nb_tryread > LXZAT_FILE_TRYREAD_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileget_req,i_nb_tryread=%d\r\n",i_nb_tryread));
                return E_LEC_ERR_P_RANGE;
            }
			
            p_cur_filectxt = &(gs_file_pool[i_file_id]);
            if (p_cur_filectxt->it_rw_status < E_LFRWS_OPEN)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileget_req,file is not open\r\n"));
                return E_LEC_ERR_NOT_ALLOWED;
            }
			
            i_read_len = fread(p_cur_filectxt->pt_rxbuf, 1, i_nb_tryread, (FILE *)p_cur_filectxt->it_fd_file);
            p_cur_filectxt->it_nb_rxdata = i_read_len;
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            break;
        }
    default:
        {
            i_err_code = E_LEC_ERR_UNSUPPORTED;
            break;
        }
    }
	
    return i_err_code;
}

/*
*  Description:
*    The response handler of "AT$MYFILEGET".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileget_resp(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_file_id = 0;
    sint32 i_read_len = 0;
    sint32 i_view_mode = 0;
    sint32 i_nb_tryread = 0;
	
    lxzat_file_t * p_cur_filectxt = NULL;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfileget_resp,p_lxzat_ctxt is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfileget_resp,p_cur_req or p_cur_spcfg is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            i_file_id = PARAM1(p_cur_req).it_number;
            if(i_file_id < 0 || i_file_id > LXZAT_FILE_NUM_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileget_resp,i_file_id=%d\r\n",i_file_id));
                return E_LEC_ERR_UNSUPPORTED;
            }
			
            i_view_mode = PARAM2(p_cur_req).it_number;
            if(i_view_mode < 0 || i_view_mode > 1)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileget_req,i_view_mode=%d\r\n",i_view_mode));
                return E_LEC_ERR_P_RANGE;
            }
			
            i_nb_tryread= PARAM3(p_cur_req).it_number;
            if(i_nb_tryread < LXZAT_FILE_TRYREAD_MIN || i_nb_tryread > LXZAT_FILE_TRYREAD_MAX)
            {
                OS_DBG_LOG(("lxzat_dollar_myfileget_resp, i_nb_tryread:=%d\r\n",i_nb_tryread));
                return E_LEC_ERR_UNSUPPORTED;
            }
			
            p_cur_filectxt = &(gs_file_pool[i_file_id]);
            i_read_len = p_cur_filectxt->it_nb_rxdata;
			
            if (i_read_len > 0)
            {
                if (i_view_mode == 1/*LXZAT_VIEW_MODE_TXT*/)
                {
                    lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                        LXZAT_RSP_MODE_P1N1,
                        "%s: %d,%d,%d",
                        p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                        i_file_id,
						i_view_mode,
                        i_read_len);
                    lxzat_rsp_f_printfhex(p_lxzat_ctxt->it_vsp_id,
                        LXZAT_RSP_MODE_P0N0,
                        p_cur_filectxt->pt_rxbuf,
                        i_read_len);
                }
                else
                {
                    lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                        LXZAT_RSP_MODE_P1N1,
                        "%s: %d,%d,%d",
                        p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                        i_file_id,
						i_view_mode,
                        i_read_len);
                    lxzat_rsp_f_printfbin(p_lxzat_ctxt->it_vsp_id,
                        LXZAT_RSP_MODE_P0N0,
                        p_cur_filectxt->pt_rxbuf,
                        i_read_len);
                }
            }
            else
            {
                lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                    LXZAT_RSP_MODE_P1N0,
                    "%s: %d,%d,%d",
                    p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                    i_file_id,
					i_view_mode,
                    0);
            }
			
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: <FileID>,<ViewMode>,<TryGetLength>",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword);
			
            break;
        }
		
    default:
        {
            break;
        }
    }
	
    return i_err_code;
}

/*
*  Description:
*    The reset handler of "AT$MYFILEGET".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfileget_reset(void * pctxt)
{
    return E_LEC_OK;
}

/*
*  Description:
*    The initialize handler of "AT$MYFILEWGET".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilewget_init(void * pctxt)
{
    LXZAT_ERROR_CODE_E e_err_code = E_LEC_ERR_MEM_NOT_ENOUGH;
    
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
    lxzat_resp_myfilewget_t * p_resp_myfilewget = NULL;

    if (p_lxzat_ctxt != NULL)
    {
        p_resp_myfilewget = (lxzat_resp_myfilewget_t *)malloc(sizeof(lxzat_resp_myfilewget_t));
        if (p_resp_myfilewget != NULL)
        {
            memset(p_resp_myfilewget, 0, sizeof(lxzat_resp_myfilewget_t));
            p_lxzat_ctxt->pt_rsp_data = p_resp_myfilewget;

            e_err_code = E_LEC_OK;
        }
    }
    
    return e_err_code;
}

/*
*  Description:
*    The request handler of "AT$MYFILEWGET".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilewget_req(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfilewget_req,p_lxzat_ctxt is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfilewget_req,p_cur_req or p_cur_spcfg is NULL\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            lxzat_resp_myfilewget_t * p_resp_myfilewget = NULL;

            p_resp_myfilewget = p_lxzat_ctxt->pt_rsp_data;
            if (p_resp_myfilewget == NULL)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewget_req,p_resp_myfilewget==NULL\r\n"));
                return E_LEC_ERR_MEM_ADDRESS;
            }

            if (p_cur_req->it_nb_param != 1)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewget_req,i_nb_param:%d\r\n",p_cur_req->it_nb_param));
                return E_LEC_ERR_P_NB_PARAM;
            }
			
            if (PARAM1_TYPE(p_cur_req) != LXZAT_PARAM_TYPE_STRING)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewget_req,PARAM1_TYPE error!\r\n"));
                return E_LEC_ERR_P_TYPE;
            }

            memset(p_resp_myfilewget->pt_fn_file, 0, LXZAT_FILE_PATH_MAX);
            lxz_dstring_get(PARAM1(p_cur_req).pt_str, p_resp_myfilewget->pt_fn_file, LXZAT_FILE_PATH_MAX);
			
            if (access(p_resp_myfilewget->pt_fn_file, F_OK) < 0)
            {
                return E_LEC_ERR_FILE_NOT_EXIST;
            }

            p_resp_myfilewget->it_fd_file = (sint32)fopen(p_resp_myfilewget->pt_fn_file, "rb");
            if (((FILE *)p_resp_myfilewget->it_fd_file) == NULL)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewget_req,fopen,fn=%s,mode=rb\r\n", p_resp_myfilewget->pt_fn_file));
				
                return E_LEC_ERR_FILE_OPEN_FAILURE;
            }
			
            break;
        }
    case LXZAT_REQ_TYPE_READ:
        {
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            break;
        }
    default:
        {
            break;
        }
    }
	
    return i_err_code;
}

/*
*  Description:
*    The response handler of "AT$MYFILEWGET".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilewget_resp(void * pctxt)
{
    sint32 i_err_code = E_LEC_OK;
	
    sint32 i_req_type = LXZAT_REQ_TYPE_ERR;
    lxzat_req_t * p_cur_req = NULL;
    lxzat_spcfg_t * p_cur_spcfg = NULL;
    lxzdata_info_t * p_cur_datacfg = NULL;
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
	
    sint32 i_id_file = 0;
    sint32 i_nb_read = 0;
    sint32 i_nb_tryread = 0;
    sint32 i_nb_allread = 0;
    sint32 i_file_size = 0;
	
    if (p_lxzat_ctxt == NULL)
    {
        OS_DBG_LOG(("lxzat_dollar_myfilewget_resp,p_lxzat_ctxt is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    p_cur_req = p_lxzat_ctxt->pt_req_data;
    p_cur_spcfg = lxzat_spcomm_f_get(p_lxzat_ctxt->it_vsp_id);
    if ((p_cur_req == NULL) || (p_cur_spcfg == NULL))
    {
        OS_DBG_LOG(("lxzat_dollar_myfilewget_resp,p_cur_req or p_cur_spcfg is NULL!\r\n"));
        return E_LEC_ERR_UNKNOWN;
    }
	
    i_req_type = p_cur_req->it_req_type;
    switch(i_req_type)
    {
    case LXZAT_REQ_TYPE_SET:
        {
            lxzat_resp_myfilewget_t * p_resp_myfilewget = NULL;
            uint08 ll_buf_data[128];

            p_resp_myfilewget = p_lxzat_ctxt->pt_rsp_data;
            if (p_resp_myfilewget == NULL)
            {
                OS_DBG_LOG(("lxzat_dollar_myfilewget_resp,p_resp_myfilewget==NULL\r\n"));
                return E_LEC_ERR_MEM_ADDRESS;
            }
			
            fseek((FILE *)p_resp_myfilewget->it_fd_file, 0, SEEK_END);
            i_file_size = ftell((FILE *)p_resp_myfilewget->it_fd_file);
            fseek((FILE *)p_resp_myfilewget->it_fd_file, 0, SEEK_SET);

            if (i_file_size > 0)
            {
                lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                    LXZAT_RSP_MODE_P1N1,
                    "%s: %d",
                    p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                    i_file_size);

                i_nb_allread = 0;
                while (i_nb_allread < i_file_size)
                {
                    i_nb_tryread = sizeof(ll_buf_data);
                    i_nb_read = fread(ll_buf_data, 1, sizeof(ll_buf_data), (FILE *)p_resp_myfilewget->it_fd_file);
                    lxzat_rsp_f_printfbin(p_lxzat_ctxt->it_vsp_id,
                        LXZAT_RSP_MODE_P0N0,
                        ll_buf_data,
                        i_nb_read);

                    i_nb_allread = i_nb_allread + i_nb_read;
                }
            }
            else
            {
                lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                    LXZAT_RSP_MODE_P1N1,
                    "%s: %d",
                    p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword,
                    0);
            }
			
            break;
        }
    case LXZAT_REQ_TYPE_TEST:
        {
            lxzat_rsp_f_printf(p_lxzat_ctxt->it_vsp_id,
                LXZAT_RSP_MODE_P1N0,
                "%s: <\"FileName\">",
                p_lxzat_ctxt->pt_ai_cfg->pt_ai_keyword);
			
            break;
        }
		
    default:
        {
            break;
        }
    }
	
    return i_err_code;
}

/*
*  Description:
*    The reset handler of "AT$MYFILEWGET".
*  Param: pctxt, the pointer of AT command context;
*  Return: Operating results;
*  Note: NONE
*/
static sint32 lxzat_dollar_myfilewget_reset(void * pctxt)
{
    LXZAT_ERROR_CODE_E e_err_code = E_LEC_ERR_MEM_NOT_ENOUGH;
    
    lxzat_context_t * p_lxzat_ctxt = (lxzat_context_t *)pctxt;
    lxzat_resp_myfilewget_t * p_resp_myfilewget = NULL;

    if (p_lxzat_ctxt != NULL)
    {
        p_resp_myfilewget = p_lxzat_ctxt->pt_rsp_data;
        if (p_resp_myfilewget != NULL)
        {
            free(p_resp_myfilewget);
            p_resp_myfilewget = NULL;

            e_err_code = E_LEC_OK;
        }
    }
    
    return e_err_code;
}

static sint32 lxzat_myfilestatus_f_get(const char * p_a_filename)
{
    sint32 i_rw_status = E_LFRWS_IDLE;
	
    sint32 i = 0;
    lxzat_file_t * p_cur_filectxt = NULL;
	
    while (i < LXZAT_FILE_NUM_MAX)
    {
        p_cur_filectxt = &(gs_file_pool[i]);
        if ((p_cur_filectxt->it_rw_status > E_LFRWS_IDLE) && (p_cur_filectxt->it_rw_status < E_LFRWS_MAX))
        {
            if (0 == strcmp(p_cur_filectxt->pt_file_name, p_a_filename))
            {
                i_rw_status = p_cur_filectxt->it_rw_status;
                break;
            }
        }
		
        i++;
    }
	
    return i_rw_status;
}

